package com.xrit.test;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Test;

import java.io.IOException;

public class ReadFormExcel {
    @Test
    public void readFromExcel() throws IOException {
        //创建工作簿，构造方法文件的路径
        Workbook workbook = new XSSFWorkbook("D:\\read.xlsx");
        //获取工作表
        Sheet sheetAt = workbook.getSheetAt(0);
        //遍历工作表获得行对象
        for (Row row : sheetAt) {
            //遍历行对象获取单元格
            for (Cell cell : row) {
                //单元格类型
                int cellType = cell.getCellType();
                if (Cell.CELL_TYPE_NUMERIC == cellType) {
                    //数值类型的单元格
                    System.out.print(cell.getNumericCellValue()+",");
                } else {
                    //从单元格取值
                    System.out.print(cell.getStringCellValue()+",");
                }
            }
            System.out.println("---------");
        }
        //关闭工作簿
        workbook.close();
    }
}
